﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Evento
/// </summary>
public partial class Evento
{   
    /// <summary>
    /// Busca o Evento do "Administrador do Evento"
    /// </summary>
    /// <param name="idUsuario">idPessoa, tabela Pessoa</param>
    /// <returns>Lista de Evento</returns>
    public static List<Evento> findEventoByIdUsuarioAdministradorEvento(Guid idPessoa)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.EventoPessoas.Where(p => p.idPessoa.Equals(idPessoa)).Select(p=>p.Evento).ToList();
    }
    public static bool excludeEvento(Guid idEvento)
    {
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Evento evento = dc.Eventos.Single(p => p.id.Equals(idEvento));
         
            dc.Eventos.DeleteOnSubmit(evento);
  
            dc.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static Evento findEventoById(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Eventos.Single(p => p.id.Equals(idEvento));
    }
    public static List<Evento> getEventoByIdAvaliador(Guid idAvaliador)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.AvaliacaoArtigos.Where(p => p.idAvaliador.Equals(idAvaliador)).Select(f => f.Evento).ToList();
    }
    public static List<Pessoa> findAdministradorEvento(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.EventoPessoas.Where(p => p.idEvento.Equals(idEvento) && p.administrador.Equals(true)).Select(p=>p.Pessoa).ToList();
    }
    public static List<Evento> findAll()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Eventos.OrderBy(p => p.dataInicial).ToList();
    }
    public static List<Evento> findEventoAberto()
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Eventos.Where(p => p.status.Equals("ativo")).ToList();
    }
    public static bool CreateEvento(Evento evento)
    {
        //try
        //{
            DataClassesDataContext dc = new DataClassesDataContext();
            dc.Eventos.InsertOnSubmit(evento);
            dc.SubmitChanges();
            return true;
        //}
        //catch
        //{
        //    return false;
        //}

    }
    public static Evento findEvento(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Eventos.Single(p => p.id.Equals(idEvento));
    }
    public static List<Quesito> findQuesitosByIdEvento(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Quesitos.Where(p => p.idEvento.Equals(idEvento)).ToList();
    }
    public static bool existQuesito(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Quesitos.Any(p => p.idEvento.Equals(idEvento));
    }
    public static bool existEvento(Guid id)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Eventos.Any(p => p.id.Equals(id));
    }
    public static bool changeEvento(Guid idEvent, string titulo, string descricao, DateTime dataFormatada, DateTime dataFormatada1, DateTime dataFormatada2, DateTime dataFormatada3, DateTime dataFormatada4, DateTime dataFormatada5)
    {
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Evento evento = dc.Eventos.Single(p => p.id.Equals(idEvent));
            evento.titulo = titulo;
            evento.descricao = descricao;
            evento.dataInicial = dataFormatada;
            evento.dataInicial = dataFormatada1;
            evento.dataInscricaoInicial = dataFormatada2;
            evento.dataInscricaoFinal = dataFormatada3;
            evento.dataArtigoInicial = dataFormatada4;
            evento.dataArtigoFinal = dataFormatada5;
            dc.SubmitChanges();
            return true;


        }
        catch (Exception ex)
        {
            return false;
        }
    }
}